//
//  AddressEditViewController.swift
//  demo
//
//  Created by toro宇 on 2020/2/22.
//  Copyright © 2020 CodeYu. All rights reserved.
//

import UIKit
import Foundation

class AddressEditViewController: UIViewController {

    // MARK: UI
    
    lazy var nav = HZCustomNavigationBar.customNavigationBar().then { (bar) in
        bar.barBackgroundColor = UIColor(hex: "fbfbfb")
        bar.shadowImageHidden = false
        /* 闭包 相当于block
         { (参数) -> (返回值) in
         
         }
         */
        bar.hz.setItemsToLeft([HZNavigationBarItem.create(normalImage: "back", barItemClickHandler: { (item) in
            self.navigationController?.popViewController(animated: true)
        }), HZNavigationBarItem.create(normalTitle: "地址编辑", barItemClickHandler: {
            (item) in
        })])
    }
    
    //地址输入
    var editTF : CustomTextField = {
        let tf = CustomTextField()
        tf.backgroundColor = UIColor.white
        tf.attributedPlaceholder = NSAttributedString(string: "请输入详细地址", attributes: [NSAttributedString.Key.foregroundColor : UIColor.gray,NSAttributedString.Key.font : UIFont.systemFont(ofSize: 13)])
        let clearBtn = UIButton()
        clearBtn .setImage(UIImage(named: "clear"), for: .normal)
        clearBtn.actionBlock = { _ in
            tf.text = ""
        }
        tf.rightViewMode = UITextField.ViewMode.always
        tf.rightView = clearBtn
        return tf
        
    }()
    
    // 地址列表
   lazy var addressTb: UITableView = {
        let tb = UITableView(frame: CGRect.zero, style: .plain)
        tb.register(AddressCell.self, forCellReuseIdentifier: "Cell")
        tb.separatorStyle = UITableViewCell.SeparatorStyle.none
        tb.delegate = self
        tb.dataSource = self
        tb.backgroundColor = UIColor.clear
        
        return tb
    }()
    
   lazy var doneBtn: UIButton = {
        let btn = UIButton()
        btn.backgroundColor  = UIColor.red
        btn.setTitle("完成", for: .normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        btn.actionBlock = { _ in
            if self.editTF.text != nil || self.editTF.text != ""{
                self.dataAry.append(self.editTF.text!)
                self.addressTb.reloadData()
            }
        }
        return btn
    }()
    
    var dataAry = ["1","2","3"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor(hex: "f4f4f4")
        view.addSubview(nav)
        view.addSubview(editTF)
        editTF.snp.makeConstraints { (make) in
        make.left.right.equalToSuperview()
            make.top.equalTo(nav.snp.bottom)
            make.height.equalTo(50)
        }
        view.addSubview(addressTb)
        addressTb.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(editTF.snp.bottom)
            make.bottom.equalToSuperview().offset(-80)
        }
        view.addSubview(doneBtn)
        doneBtn.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(15)
            make.right.equalToSuperview().offset(-15)
            make.bottom.equalToSuperview().offset(-25)
            make.height.equalTo(45)
        }
        
        
        // Do any additional setup after loading the view.
    }


    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        editTF.resignFirstResponder()
    }

}

//MARK: --TBDELEGSTE
extension AddressEditViewController: UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataAry.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! AddressCell
        cell.deleteBlock = {
            self.dataAry.remove(at: indexPath.row)
            self.addressTb.reloadData()
        }
        
        cell.configUI(dataStr: dataAry[indexPath.row])
        
        return cell
        
    }
    
    
}


//MARK: --自定义TextFiled
class CustomTextField: UITextField{
    // 占位符位置
    override func placeholderRect(forBounds bounds: CGRect) -> CGRect {
        return CGRect(origin: CGPoint(x: bounds.origin.x + 15, y: bounds.origin.y), size: bounds.size)
    }
    // 文本输入完成时位置
    override func textRect(forBounds bounds: CGRect) -> CGRect {
        return CGRect(origin: CGPoint(x: bounds.origin.x + 15, y: bounds.origin.y), size: CGSize(width: bounds.width - 50, height: bounds.height))
    }
    //文本正在输入时位置
    override func editingRect(forBounds bounds: CGRect) -> CGRect {
        return CGRect(origin: CGPoint(x: bounds.origin.x + 15, y: bounds.origin.y), size: CGSize(width: bounds.width - 50, height: bounds.height))
    }
    
    override func rightViewRect(forBounds bounds: CGRect) -> CGRect {
        return CGRect(origin: CGPoint(x: bounds.origin.x + bounds.size.width - 35 , y: bounds.origin.y + 15), size: CGSize(width:25, height: 25))
    }
}
